package com.ruoyi.web.controller.laiqianba.source;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.db.domain.SourceDataCountInfo;
import com.ruoyi.source.service.ISourceDataCountInfoService;
import com.ruoyi.web.wrapper.SourceBaseInfoWrapper;
import com.ruoyi.web.wrapper.SourceDataCountInfoWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 线索数据统计Controller
 *
 * @author ruoyi
 * @date 2023-12-28
 */
@RestController
@RequestMapping("/source/dataCount")
public class SourceDataCountInfoController extends BaseController {
    @Autowired
    private ISourceDataCountInfoService sourceDataCountInfoService;

    /**
     * 查询线索数据统计列表
     */
    @PreAuthorize("@ss.hasPermi('source:dataCount:list')")
    @GetMapping("/list")
    public TableDataInfo list(SourceDataCountInfo sourceDataCountInfo) {

        /**
         * 正常列表查询
         */
        startPage();
        List<Map<String, Object>> list = sourceDataCountInfoService.selectSourceDataCountInfoMapList(sourceDataCountInfo);
        if (list == null || list.isEmpty()) {
            return getDataTable(list);
        }
        list = new SourceDataCountInfoWrapper(list).wrap();
        Map<String, Object> AllDataCount = sourceDataCountInfoService.selectSourceAllDataCountInfoMapList2(sourceDataCountInfo);


        BigDecimal totalShouldNoticeNumber = BigDecimal.ZERO;

        for (Map<String, Object> item : list) {
            BigDecimal shouldNoticeNumber = (BigDecimal) item.get("shouldNoticeNumber");
            if (shouldNoticeNumber != null) {
                totalShouldNoticeNumber = totalShouldNoticeNumber.add(shouldNoticeNumber);
            }
        }

        // 将累加结果放入 AllDataCount 中
        AllDataCount.put("shouldNoticeNumber", totalShouldNoticeNumber);

        AllDataCount = new SourceDataCountInfoWrapper(AllDataCount).wrap();

        list.add(0, AllDataCount);


        return getDataTable(list);
    }

    /**
     * 导出线索数据统计列表
     */
    @PreAuthorize("@ss.hasPermi('source:dataCount:export')")
    @Log(title = "线索数据统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SourceDataCountInfo sourceDataCountInfo) {
        List<SourceDataCountInfo> list = sourceDataCountInfoService.selectSourceDataCountInfoList(sourceDataCountInfo);
        ExcelUtil<SourceDataCountInfo> util = new ExcelUtil<SourceDataCountInfo>(SourceDataCountInfo.class);
        util.exportExcel(response, list, "线索数据统计数据");
    }

    /**
     * 获取线索数据统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('source:dataCount:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sourceDataCountInfoService.selectSourceDataCountInfoById(id));
    }

    /**
     * 新增线索数据统计
     */
    @PreAuthorize("@ss.hasPermi('source:dataCount:add')")
    @Log(title = "线索数据统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SourceDataCountInfo sourceDataCountInfo) {
        return toAjax(sourceDataCountInfoService.insertSourceDataCountInfo(sourceDataCountInfo));
    }

    /**
     * 修改线索数据统计
     */
    @PreAuthorize("@ss.hasPermi('source:dataCount:edit')")
    @Log(title = "线索数据统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SourceDataCountInfo sourceDataCountInfo) {
        return toAjax(sourceDataCountInfoService.updateSourceDataCountInfo(sourceDataCountInfo));
    }

    /**
     * 删除线索数据统计
     */
    @PreAuthorize("@ss.hasPermi('source:dataCount:remove')")
    @Log(title = "线索数据统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sourceDataCountInfoService.deleteSourceDataCountInfoByIds(ids));
    }
}
